import string;
from Matrix import Matrix 
from mfuncs import size
from jxl import *
from jxl.write import *;
import java,javax,jarray;

def typeChk(x):
	if x=='NA' or x =='':
		return float(0)
	else:
		return float(x)

def writeMatExcel(filename,A,P):
    (pr,pc)=size(P);
    (ar,ac)=size(A);
    workbook = Workbook.createWorkbook(java.io.File(filename))   
    sheet1 = workbook.createSheet(P._name, 0);
    sheet2 = workbook.createSheet(A._name, 1);
    
#set up the labels
    y=1
    for x in P.rowNames: 
        sheet1.addCell(Label(0,y,x));
        y=y+1
    y=1
    for x in P.columnNames[1:len(P.columnNames)]: 
        label = Label(y,0,x);
        sheet1.addCell(label);
        y=y+1      
  
    for i in range(1,pr+1):
        for j in range(1,pc+1):
            sheet1.addCell(Number(j, i, P[i,j]._M.get(0)))
   # workbook.write()
    y=1
    for x in A.rowNames: 
        sheet2.addCell(Label(0,y,x));
        y=y+1
    y=1
    for x in A.columnNames[1:len(A.columnNames)]: 
        label = Label(y,0,x);
        sheet2.addCell(label);
        y=y+1       
    for i in range(1,ar+1):
        for j in range(1,ac+1):
            sheet2.addCell(Number(j, i, A[i,j]._M.get(0)))

    workbook.write()
    workbook.close()
    print "Saved Excel File..."

def readExcel(filename,progressManager,sheet=None):
	if sheet is None:
		sheet=0
	instream = java.io.FileInputStream(java.io.File(filename))
	plines = javax.swing.ProgressMonitorInputStream(progressManager,'Loading File', instream)
	workbook = Workbook.getWorkbook(plines)   
	s1 = workbook.getSheet(sheet);
	r=s1.getRows()
	c=s1.getColumns()
	M=Matrix(r-1,c-1,0)
	Clabels=[]
	Mlabels=[]
	for x in xrange(0,r):
		for y in xrange(0,c):
			if x==0:
				Clabels+=[s1.getCell(y,x).getContents()]
			elif y==0:
				Mlabels+=[s1.getCell(y,x).getContents()]
			else:
				M[x,y]=typeChk(s1.getCell(y,x).getContents())

	M.rowNames=Mlabels
	M.columnNames=Clabels
	workbook.close()
	return (M,Mlabels,Clabels)

def readmatrix(filename,type=None,progressManager=None,sparse=False):
  instream = java.io.FileInputStream(java.io.File(filename))
  if progressManager is None:
	  plines=java.io.InputStreamReader(instream)
  else:
	  plines = java.io.InputStreamReader(javax.swing.ProgressMonitorInputStream(progressManager,'Loading File', instream))
	  
  lines=java.io.BufferedReader(plines)
  ridx=0;
  cidx=0;
  line = lines.readLine()
  Q = string.split(line,'\t')
  CLabels=[]
  CLabels=Q[1:len(Q)]
  # first line is header
  m=[]
  MLabels=[]
  R=1
  Cx=len(CLabels)+1
  while line != None:
	  line = lines.readLine()
	  if line is None:
		  break
	  line = string.strip(line);
	  Q = string.split( line, '\t' );
	  MLabels += [Q[0]]
	  if len(Q) == Cx:
		  Z=jarray.array(map( typeChk,Q[1:Cx]),'d')
		  m += [Z]
	  #else:
	#	  pass
	  R = R+1
  m = jarray.array(m,java.lang.Class.forName("[D"))
  M=Matrix(R-1,len(CLabels),m)
  if type=="connectivity":
    M._name="A"
  else:
   	M._name="signal"
   
  M.columnNames=[' '] + CLabels
  M.rowNames=MLabels
  return (M,MLabels,CLabels)
